<template>
    <el-dialog :title="$t('Password')" v-model="pwdDialogShow" width="600px">
        <el-form :model="pwdForm" :rules="rules" label-width="100px" ref="refForm">
            <el-form-item :label="$t('currentPwd')" prop="old">
                <el-input v-model="pwdForm.old" :placeholder="$t('input_placeholder')" type="password"
                    show-password></el-input>
            </el-form-item>
            <el-form-item :label="$t('newPwd')" prop="new1">
                <el-input v-model="pwdForm.new1" :placeholder="$t('input_placeholder')" type="password"
                    show-password></el-input>
            </el-form-item>
            <el-form-item :label="$t('confirmPwd')" prop="new2">
                <el-input v-model="pwdForm.new2" :placeholder="$t('input_placeholder')" type="password"
                    show-password></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="save">{{ $t('Save') }}</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>
  
<script setup>
import i18n from '@/lang'
import { nextTick } from 'vue';
import FNUC from "@/api/func"
import { tansParams } from '@/utils/ruoyi'
import { useI18n } from 'vue-i18n'

const { locale } = useI18n()

const pwdDialogShow = ref(false)
const pwdForm = reactive({})
const rules = reactive({
    old: [{ required: true, message: i18n.global.t('Cannot be empty'), trigger: 'blur' }],
    new1: [{ required: true, message: i18n.global.t('Cannot be empty'), trigger: 'blur' }],
    new2: [{ validator: checkPwd, trigger: 'blur' }],
})
function checkPwd(rule, value, callback) {
    if (!value) {
        callback(new Error(i18n.global.t('Cannot be empty')))
    } else if (value != pwdForm.new1) {
        callback(new Error(i18n.global.t('Passwords does not match!')))
    } else {
        callback()
    }
}

function open() {
    pwdDialogShow.value = true
    nextTick(() => {
        refForm.value.resetFields()
    })
}
const refForm = ref(null)
function save() {
    refForm.value.validate(valid => {
        if (valid) {
            FNUC('setPasswd', pwdForm).then(res => {
                pwdDialogShow.value = false
            })
        }
    })
}

watch(() => {
    return locale.value
}, (newValue, oldValue) => {
    Object.assign(rules, {
        old: [{ required: true, message: i18n.global.t('Cannot be empty'), trigger: 'blur' }],
        new1: [{ required: true, message: i18n.global.t('Cannot be empty'), trigger: 'blur' }],
    })
});

defineExpose({
    open
})
</script>
  
<style lang='scss' scoped></style>
  